<?php

namespace App\Http\Controllers\Admin;

use App\Exports\FangownerExport;
use App\Jobs\CreateExcelFangowner;
use App\Models\Fangowner;
use Illuminate\Http\Request;
use App\Http\Requests\FangownerRequest;
use Maatwebsite\Excel\Facades\Excel;

class FangownerController extends BaseController
{
    //房东列表
    public function index(Fangowner $fangowner)
    {
        $data=$fangowner->orderBy('id','desc')->paginate($this->pagesize);
        return view('admin.fangowner.index',compact('fangowner','data'));
    }
    //生成excel
    public function createexcel(){
        //投递一个生成excel任务
        $this->dispatch(new CreateExcelFangowner());
        return redirect(route('admin.fangowner.index'));
    }
    //导出excel
    public function excel(){
        //导出
        return Excel::download(new FangownerExport(),'fangdong.xlsx');
        //服务端生成
//        Excel::store(new FangownerExport(),'fangdong.xlsx','excel');
    }
    //添加房东页面显示
    public function create()
    {
        return view('admin.fangowner.create');
    }

    //添加房东数据接收处理
    public function store(FangownerRequest $request)
    {
        $data=$request->except(['_token','file']);
        $model=Fangowner::create($data);
        //给添加成功的房东发送邮件
//        if($model){
//            //添加房东成功
//            //同步发送文本邮件
//            Mail::raw('您的信息在本平台添加成功',function (Message $message) use($model){
//                $message->to($model->email,$model->name);
//                $message->subject('添加成功通知');
//            });
//        }
        return redirect(route('admin.fangowner.index'))->with('success','添加成功');
    }

    //查看身份证照片
    public function show(Fangowner $fangowner)
    {
        return view('admin.fangowner.show',compact('fangowner'));
    }

    //修改房东显示
    public function edit(Fangowner $fangowner)
    {
        return view('admin.fangowner.edit',compact('fangowner'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Fangowner  $fangowner
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Fangowner $fangowner)
    {
        $data=$request->except(['_token','_method','file']);
        //更新到数据库
        $fangowner->update($data);
        //返回列表页
        return redirect(route('admin.fangowner.index'))->with('success','修改成功');
    }
    //删除图片
    public function delpic(Request $request)
    {
        $path=$request->get('path');
        $id=$request->get('id');
        //隐藏域实时数据
        $pic=$request->get('pic');
        //删除图片操作
        $filepath=public_path($path);
        //判断此文件是否存在，存在则删除
        if(file_exists($filepath)){
            unlink($filepath);
        }
        //修改数据表中的字段信息
        $model=Fangowner::find($id);
        //修改字段值
        $data['pic']=str_replace('#'.$path,'',$pic);
        //更新到数据表中
        $model->update($data);
        return ['code'=>200,'msg'=>'删除图片成功','pic'=>$data['pic']];
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Fangowner  $fangowner
     * @return \Illuminate\Http\Response
     */
    public function destroy(Fangowner $fangowner)
    {
        //
    }
}
